package com.example.fasttask;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TaskTranslator {
	// Database fields
		public SQLiteDatabase database;
		public TaskSQLiteHelper helper;
		public String[] allColumns = { TaskSQLiteHelper.COLUMN_ID,
				TaskSQLiteHelper.COLUMN_TASK,TaskSQLiteHelper.COLUMN_DATE,TaskSQLiteHelper.COLUMN_RECUR, TaskSQLiteHelper.COLUMN_LENGTH };
		
		public TaskTranslator(Context context) {
			helper = new TaskSQLiteHelper(context);
		}
		
		public void open() throws SQLException {
			database = helper.getWritableDatabase();
		}
		
		public void close() {
			helper.close();
		}
		
		public Task createTask(String name, int length, Date date, boolean isRecurring) {
			Log.d("q", "creating task");
			int recur;
			if(isRecurring) recur = 1;
			else recur = 0;
			ContentValues values = new ContentValues();
			values.put(TaskSQLiteHelper.COLUMN_TASK, name);
			values.put(TaskSQLiteHelper.COLUMN_LENGTH, length);
			
			//Log.e("asdf", date.toString());
			
			DateFormat df = DateFormat.getDateInstance();
			
			values.put(TaskSQLiteHelper.COLUMN_DATE, df.format(date));
			values.put(TaskSQLiteHelper.COLUMN_RECUR, recur);
			long insertId = database.insert(TaskSQLiteHelper.TABLE_TASKS, null,
					values);
			Cursor cursor = database.query(TaskSQLiteHelper.TABLE_TASKS,
					allColumns, TaskSQLiteHelper.COLUMN_ID + " = " + insertId, null,
					null, null, null);
			cursor.moveToFirst();
			Task newTask= cursorToTask(cursor);
			cursor.close();
			return newTask;
		}
		
		public void deleteTask(Task t) {
			long id = t.getId();
			System.out.println("Task deleted with id: " + id);
			database.delete(TaskSQLiteHelper.TABLE_TASKS, TaskSQLiteHelper.COLUMN_ID
					+ " = " + id, null);
		}
		
		public List<Task> getAllTasks() {
			List<Task> tasks = new ArrayList<Task>();

			Cursor cursor = database.query(TaskSQLiteHelper.TABLE_TASKS,
					allColumns, null, null, null, null, null);

			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				Task t = cursorToTask(cursor);
				tasks.add(t);
				cursor.moveToNext();
			}
			// Make sure to close the cursor
			cursor.close();
			return tasks;
		}
		
		public Task cursorToTask(Cursor cursor) {
			Task t;
			//String format = "E L dd HH:mm:ss z yyyy";
			
			try {
				DateFormat df = DateFormat.getDateInstance();
				//SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
				
				df.setLenient(true);
				//Log.e("asdf", cursor.getString(2));
				t = new Task(cursor.getString(1), cursor.getInt(4),
						df.parse(cursor.getString(2)), (cursor.getInt(3) == 1)? true: false);
			} catch (Exception e) {
				Log.e("asdf","Error reading from database", e);
				t = new Task("Go outside", 5, new Date(), true);
			}
			t.setId(cursor.getLong(0));
			return t;
		}

}
